use "$datapath/A3_gen_empmark.dta",clear

*Dropping dates prior to  and after 2014m12 (since we want to follow people for at least 2 years)
drop if notdate_def<=ym(2004,12) | notdate_def>ym(2016,12)

*Dropping small notifications
drop if antvars<5

*Dropping negative notification times
keep if inkom_dat<anstupp_dat

*Generate eventtime relative to first report
gen eventtime_anstupp = date - ym(year(anstupp_dat) , month(anstupp_dat))
gen eventtime_ankomst = date - ym(year(ankomst_dat) , month(ankomst_dat))


merge 1:1 persid lopnr eventtime_ankomst using "$datapath/A3_gen_empmark_ankomst.dta"
drop if _merge==2
drop _merge

// Normalize control variables 
*===============================================================================
foreach var of   varlist annual_ear_prenot_def female immigrant tenureatnot educ0 educ1 educ2 educ3 educ4 age_def {
	sum `var' 	
	gen C_`var' = `var' - r(mean)
}	
*



{ // Indicate first observations of new job
*===============================================================================
*Condtion on newfirm==1 and observred wage (post eventtime>=0 by construction)
bys lopnr (eventtime_ankomst) : gen nvals = sum(newfirmid_ankomst!=. & manl_newfirm_ankomst!=. & newfirmid_ankomst==firstnewfirmid_ankomst) if newfirmid!=. & manl_newfirm_ankomst!=. & newfirmid_ankomst==firstnewfirmid_ankomst
gen xnvalsdate = date if nvals==1 
bys lopnr (eventtime_ankomst) : egen nvalsdate = max(xnvalsdate) // impute for all obs this value
format nvalsdate %tm

/*
*Generte same as above but correct for observing the wage possibly earlier
bys lopnr (eventtime) : gen nvals2 = sum(1) if firmid_lonestruktur==firstnewfirmid & eventtime_def>=0 & firmid_lonestruktur!=.
gen xnvals2date = date if nvals2==1 
bys lopnr (eventtime_def) : egen nvals2date = max(xnvals2date) // impute for all obs this value
format nvals2date %tm

*Indicate first wage from new firm in wage survey (no matter the firm and RAMS indicators)
bys lopnr (eventtime) : gen nvals4 = sum(1) if firmid!=firmid_lonestruktur & firmid_lonestruktur!=. & eventtime_def>=0
*/


*Pre wage from notifying firm (last observred wage at notifying firm before or at notification)
bys lopnr (eventtime_def) : gen nvals3 = _n if manl!=. & date<=nvalsdate & eventtime_def<=0
bys lopnr (eventtime_def) : egen maxnvals3 = max(nvals3)


*Date for last wage at notifying firm (within 2 yers before notification)
gen xnvals3date = date if nvals3==maxnvals3	& nvals3!=. & inrange(eventtime_def,-24,0)
bys lopnr (eventtime_def) : egen nvals3date = max(xnvals3date)
format nvals3date %tm

*Difference in time between old and new wage
gen diff = nvalsdate - nvals3date
sum diff if nvals==1

*Wage at notifying firm	
gen xprewage = manl if date==nvals3date 
bys lopnr (eventtime_def) : egen prenotfwage = max(xprewage)
drop xprewage



}
*



{ // Generate wages
*===============================================================================
*Generate log wage variables 
gen log_manl_newfirm_ankomst = log(manl_newfirm_ankomst)
gen diff_log_manl_newfirm_ankomst = log(manl_newfirm_ankomst) - log(prenotfwage)

}
*


*save "$datapath/temp.dta",replace
*use "$datapath/temp.dta",clear

keep if  inrange(eventtime_ankomst,-12,12)




{ // Generate De jure notification time (DJNT)
*===============================================================================
gen 	de_jure_NT = .
replace de_jure_NT = 1 if inrange(tenureatnot,0,23) 
replace de_jure_NT = 2 if inrange(tenureatnot,24,47) 
replace de_jure_NT = 3 if inrange(tenureatnot,48,71) 
replace de_jure_NT = 4 if inrange(tenureatnot,72,95) 
replace de_jure_NT = 5 if inrange(tenureatnot,96,119) 
replace de_jure_NT = 6 if tenureatnot>=120 & tenureatnot!=. 
replace de_jure_NT = 12 if age_def>=55 & tenureatnot>=10*12 & tenureatnot!=. & WC==1

count if de_jure_NT==. 	
count if tenureatnot==. 	

}
*

{ // Generate time between first layoff report and indiviual notification
gen ankomst_inkom_time 	= (inkom_dat - ankomst_dat)/30
sum ankomst_inkom_time if eventtime_def==0,d
drop if ankomst_inkom_time<0 			// about 12,000 individuals 
gen ankomst_inkom_time_round = round(ankomst_inkom_time)
gen ankomst_inkom_time_ceil  = ceil(ankomst_inkom_time)
gen ankomst_inkom_time_floor = floor(ankomst_inkom_time)

sum ankomst_inkom_time* if eventtime_def==0,d
}
*

{ // Generate hazard relative to first report
*===============================================================================
*Hazard in t
bys lopnr (eventtime_ankomst) : gen haz = sum(otherfirm) 
bys lopnr (eventtime_ankomst) : egen maxmax = max(haz==1 & eventtime_ankomst==-6)
replace haz = . if maxmax==1
bys lopnr (eventtime_ankomst) : replace haz = sum(haz) if maxmax!=1
replace haz = . if haz>1 
drop maxmax

*Hazard in t+1
bys lopnr (eventtime_ankomst) : gen haz_tp1 = sum(otherfirm[_n+1]) 
bys lopnr (eventtime_ankomst) : egen maxmax = max(haz_tp1==1 & eventtime_ankomst==-7)
replace haz_tp1 = . if maxmax==1
bys lopnr (eventtime_ankomst) : replace haz_tp1 = sum(haz_tp1) if maxmax!=1
replace haz_tp1 = . if haz_tp1>1 
drop maxmax 


*Hazard in t 
bys lopnr (eventtime_ankomst) : gen haz_v2 = sum(otherfirm2_ankomst)
replace haz_v2 = . if haz_v2>1 
replace haz_v2 = haz if eventtime_ankomst<0

*Hazard in t+1
bys lopnr (eventtime_ankomst) : gen haz_tp1_v2 = sum(otherfirm2_ankomst[_n+1]) 
replace haz_tp1_v2 = . if haz_tp1_v2>1 
replace haz_tp1_v2 = haz_tp1 if eventtime_ankomst<0

}
*


*Genreate search intensity
merge m:1 persid date using "$datapath/AKUsearch.dta"
drop  if _merge==2
drop _merge

gen 	search_ind = max(search_databa, search_direct, search_postads, search_recruit)
replace search_ind = 0  if search!=. & search_ind!=1
replace search_int = 0 if search!=. & search_int==.
gen 	search_pes = max(search_afcoach, search_afself)
replace search_pes = 0 if search!=. & search_pes==.
gen search_int_arc = ln(search_int + sqrt(search_int^2 +1))

*Generate maximum follow up time
gen followup_round = de_jure_NT + ankomst_inkom_time_round
gen followup_ceil = de_jure_NT + ankomst_inkom_time_ceil
gen followup_floor = de_jure_NT + ankomst_inkom_time_floor

sum follow* if inrange(nottime_def,30,180) & eventtime_ankomst==0,d

gen C_age_def_sq = C_age_def^2


{ // Define globals
*===============================================================================
global covariets 	" C_annual_ear_prenot_def C_female C_immigrant  C_educ1 C_educ2 C_educ3 C_educ4 C_tenureatnot C_age_def"		
global instrument	"treat"
global linear		"runvar inter"
global quadratic	"$linear q_runvar q_inter"
global cubic 		"$quadratic c_runvar c_inter"
}
*

{ // Follow from first report to max 2 months into notice (with "loose" hazard) 
*===============================================================================
preserve
	keep if inrange(de_jure_NT,2,6)
	keep if inrange(eventtime_ankomst,0,ankomst_inkom_time_round+2)
	drop if varselorsak==5
	
	replace annual_ear_prenot_def = annual_ear_prenot_def/1000
	replace manl_prenot_def = manl_prenot_def/1000
	eststo: estpost sum female immigrant age_def tenureatnot annual_ear_prenot_def manl_prenot_def educ1 educ2 educ3 if haz_tp1!=.
	esttab , cells("mean(fmt(2))")
	*Graph bisncatters
	*binscatter haz_tp1	 de_jure_NT 		  ,nq(60) name(a2,replace) controls($covariets ) absorb(ym_def) reportreg ytitle("Hazard t+1")
	*graph export "$agegraphpath/Search/BS_haz.pdf",replace
	*binscatter search_ind  de_jure_NT if haz_tp1!=.   ,nq(60) name(b2,replace) controls($covariets ) absorb(ym_def) reportreg ytitle("Search")
	*graph export "$agegraphpath/Search/BS_search.pdf",replace
	*binscatter diff_log_manl_newfirm_ankomst  de_jure_NT   if nvals==1 ,nq(60) name(b2,replace) controls($covariets ) absorb(ym_def) reportreg ytitle("{&Delta log(w)}")
	*graph export "$agegraphpath/Search/BS_wage.pdf",replace
	
		
	
	
	*Reduced form
		eststo clear	
		eststo: qui reghdfe haz_tp1		  		de_jure_NT 	 $covariets  			, cluster(persid) absorb(ym_def)
		esttab using "$agetablepath/Search/col1_row1.tex",  notype b(4) se(4) keep(de_jure_NT)  replace wide ///
		nonotes noobs nolab nomtitle nodepvar nonumber prehead("") posthead("") prefoot("")  postfoot("") end("") begin("") ///
		varlab(de_jure_NT "Hazard")  collabels(none) star(* 0.1 ** 0.05 *** 0.01)
		
		eststo clear	
		eststo: qui reghdfe diff_log_manl_newfirm_ankomst	de_jure_NT 	 $covariets  if nvals==1	, cluster(persid) absorb(ym_def)
		esttab using "$agetablepath/Search/col1_row2.tex",  notype b(4) se(4) keep(de_jure_NT)  replace wide ///
		nonotes noobs nolab nomtitle nodepvar nonumber prehead("") posthead("") prefoot("")  postfoot("") end("") begin("") ///
		varlab(de_jure_NT "ln( $ w $ )")  collabels(none) star(* 0.1 ** 0.05 *** 0.01)
		
		eststo clear	
		eststo: qui reghdfe search		  		de_jure_NT 	 $covariets  if haz_tp1!=.	, cluster(persid) absorb(ym_def)
		esttab using "$agetablepath/Search/col1_row3.tex",  notype b(4) se(4) keep(de_jure_NT)  replace wide ///
		nonotes noobs nolab nomtitle nodepvar nonumber prehead("") posthead("") prefoot("")  postfoot("") end("") begin("") ///
		varlab(de_jure_NT "Total")  collabels(none) star(* 0.1 ** 0.05 *** 0.01)
		
		eststo clear	
		eststo: qui reghdfe search_ind	  		de_jure_NT 	 $covariets  if haz_tp1!=.	, cluster(persid) absorb(ym_def)
		esttab using "$agetablepath/Search/col1_row4.tex",  notype b(4) se(4) keep(de_jure_NT)  replace wide ///
		nonotes noobs nolab nomtitle nodepvar nonumber prehead("") posthead("") prefoot("")  postfoot("") end("") begin("") ///
		varlab(de_jure_NT "Outside PES")  collabels(none) star(* 0.1 ** 0.05 *** 0.01)
		
		eststo clear	
		eststo: qui reghdfe search_int_arc		  	de_jure_NT 	 $covariets  if haz_tp1!=.	, cluster(persid) absorb(ym_def)
		esttab using "$agetablepath/Search/col1_row5.tex",  notype b(4) se(4) keep(de_jure_NT)  replace wide ///
		nonotes noobs nolab nomtitle nodepvar nonumber prehead("") posthead("") prefoot("")  postfoot("") end("") begin("") ///
		varlab(de_jure_NT "Arcsine")  collabels(none) star(* 0.1 ** 0.05 *** 0.01)
	
	*2-sample IV
	matrix B = J(3,3,.)
	eststo clear 
		qui reghdfe 	search 		  		de_jure_NT 	 $covariets  if haz_tp1!=.	, cluster(persid) absorb(ym_def)
			predict search_hat,xb
			scalar s1=e(rmse)^2
		eststo: reghdfe haz_tp1		  		search_hat 	 $covariets  	, cluster(persid) absorb(ym_def)
			matrix B[1,1] = _b[search_hat]
			matrix V = e(V)
			scalar correct=sqrt(1+_b[search_hat]^2*s1/(e(rmse)^2))
			di "SE of coefficient on search1 = " correct*sqrt(V[1,1]) // plug in manually in table
			matrix B[1,2] = correct*sqrt(V[1,1])
		 drop search_hat
		esttab using "$agetablepath/Search/col1_row6.tex",  notype b(4) se(4) keep(search_hat)  replace wide ///
		nonotes noobs nolab nomtitle nodepvar nonumber prehead("") posthead("") prefoot("")  postfoot("") end("") begin("") ///
		varlab(search_hat "Total")  collabels(none) star(* 0.1 ** 0.05 *** 0.01)
		
	eststo clear 	 
		qui reghdfe 	search_ind		  		de_jure_NT 	 $covariets  if haz_tp1!=.	, cluster(persid) absorb(ym_def)		
		predict search_hat,xb
			scalar s1=e(rmse)^2
		eststo: reghdfe haz_tp1		  		search_hat 	 $covariets  	, cluster(persid) absorb(ym_def)
			matrix B[2,1] = _b[search_hat]
			matrix V = e(V)
			scalar correct=sqrt(1+_b[search_hat]^2*s1/(e(rmse)^2))
			di "SE of coefficient on search1 = " correct*sqrt(V[1,1]) // plug in manually in table
			matrix B[2,2] = correct*sqrt(V[1,1])
			
		 drop search_hat 
		esttab using "$agetablepath/Search/col1_row7.tex",  notype b(4) se(4) keep(search_hat)  replace wide ///
		nonotes noobs nolab nomtitle nodepvar nonumber prehead("") posthead("") prefoot("")  postfoot("") end("") begin("") ///
		varlab(search_hat "Outside PES")  collabels(none) star(* 0.1 ** 0.05 *** 0.01)

	eststo clear 
		 qui reghdfe search_int_arc		  		de_jure_NT 	 $covariets  if haz_tp1!=.	, cluster(persid) absorb(ym_def)
			predict search_hat,xb
			scalar s2=e(rmse)^2
			
		eststo: reghdfe haz_tp1		  			search_hat 	 $covariets  	, cluster(persid) absorb(ym_def)
			matrix B[3,1] = _b[search_hat]
			matrix V = e(V)
			scalar correct=sqrt(1+_b[search_hat]^2*s2/(e(rmse)^2))
			di "SE of coefficient on search2 = " correct*sqrt(V[1,1]) // plug in manually in table
			matrix B[3,2] = correct*sqrt(V[1,1])
			
		 drop search_hat 
		esttab using "$agetablepath/Search/col1_row8.tex",  notype b(4) se(4) keep(search_hat)  replace wide ///
		nonotes noobs nolab nomtitle nodepvar nonumber prehead("") posthead("") prefoot("")  postfoot("") end("") begin("") ///
		varlab(search_hat "Arcsine")  collabels(none) star(* 0.1 ** 0.05 *** 0.01)

		
matrix list B		
svmat B
keep in 1/3
keep B1-B3
tostring B3,replace
replace B3 = "Extensive search" in 1
replace B3 = "Outside PES search" in 2
replace B3 = "Search intensity" in 3
rename B1 b
rename B2 se
rename B3 meassure
gen analysis = "De jure"

save "$datapath/Searchint_meassures_dejure.dta",replace


restore	

}
*

/*


{ // Follow from first report to max 2 months into notice (with "loose" hazard) 
*===============================================================================
preserve
	keep if inrange(de_jure_NT,2,6)
	keep if inrange(eventtime_ankomst,0,ankomst_inkom_time_round+2)
	drop if varselorsak==5
	
	
	*Graph bisncatters
	*binscatter haz_tp1	 de_jure_NT 		  ,nq(60) name(a2,replace) controls($covariets ) absorb(ym_def) reportreg ytitle("Hazard t+1")
	*graph export "$agegraphpath/Search/BS_haz.pdf",replace
	*binscatter search_ind  de_jure_NT if haz_tp1!=.   ,nq(60) name(b2,replace) controls($covariets ) absorb(ym_def) reportreg ytitle("Search")
	*graph export "$agegraphpath/Search/BS_search.pdf",replace
	*binscatter diff_log_manl_newfirm_ankomst  de_jure_NT   if nvals==1 ,nq(60) name(b2,replace) controls($covariets ) absorb(ym_def) reportreg ytitle("{&Delta log(w)}")
	*graph export "$agegraphpath/Search/BS_wage.pdf",replace
	
		
	eststo clear	
		eststo: qui reghdfe haz_tp1		  		de_jure_NT 	 $covariets  			, cluster(persid) absorb(ym_def)
		eststo: qui reghdfe diff_log_manl_newfirm_ankomst	de_jure_NT 	 $covariets  if nvals==1	, cluster(persid) absorb(ym_def)
		eststo: qui reghdfe search_ind		  		de_jure_NT 	 $covariets  if haz_tp1!=.	, cluster(persid) absorb(ym_def)
		eststo: qui reghdfe search		  		de_jure_NT 	 $covariets  if haz_tp1!=.	, cluster(persid) absorb(ym_def)
		eststo: qui reghdfe search_int		  		de_jure_NT 	 $covariets  if haz_tp1!=.	, cluster(persid) absorb(ym_def)
		eststo: qui reghdfe search_int_arc		  	de_jure_NT 	 $covariets  if haz_tp1!=.	, cluster(persid) absorb(ym_def)

		
		qui reghdfe 	search_ind		  		de_jure_NT 	 $covariets  if haz_tp1!=.	, cluster(persid) absorb(ym_def)
			predict search_hat,xb
			scalar s1=e(rmse)^2
		eststo: reghdfe haz_tp1		  		search_hat 	 $covariets  	, cluster(persid) absorb(ym_def)
			matrix V = e(V)
			scalar correct=sqrt(1+_b[search_hat]^2*s1/(e(rmse)^2))
			di "SE of coefficient on search1 = " correct*sqrt(V[1,1]) // plug in manually in table
		 drop search_hat
		 
		qui reghdfe 	search		  		de_jure_NT 	 $covariets  if haz_tp1!=.	, cluster(persid) absorb(ym_def)		
		predict search_hat,xb
			scalar s1=e(rmse)^2
		eststo: reghdfe haz_tp1		  		search_hat 	 $covariets  	, cluster(persid) absorb(ym_def)
			matrix V = e(V)
			scalar correct=sqrt(1+_b[search_hat]^2*s1/(e(rmse)^2))
			di "SE of coefficient on search1 = " correct*sqrt(V[1,1]) // plug in manually in table
		 drop search_hat 
		 
		 qui reghdfe search_int		  		de_jure_NT 	 $covariets  if haz_tp1!=.	, cluster(persid) absorb(ym_def)
			predict search_hat,xb
			scalar s2=e(rmse)^2
		
		eststo: reghdfe haz_tp1		  			search_hat 	 $covariets  	, cluster(persid) absorb(ym_def)
			matrix V = e(V)
			scalar correct=sqrt(1+_b[search_hat]^2*s2/(e(rmse)^2))
			di "SE of coefficient on search2 = " correct*sqrt(V[1,1]) // plug in manually in table
		 drop search_hat 
		 
		 qui reghdfe search_int_arc		  		de_jure_NT 	 $covariets  if haz_tp1!=.	, cluster(persid) absorb(ym_def)
			predict search_hat,xb
			scalar s2=e(rmse)^2
		
		eststo: reghdfe haz_tp1		  			search_hat 	 $covariets  	, cluster(persid) absorb(ym_def)
			matrix V = e(V)
			scalar correct=sqrt(1+_b[search_hat]^2*s2/(e(rmse)^2))
			di "SE of coefficient on search2 = " correct*sqrt(V[1,1]) // plug in manually in table
		 drop search_hat 		
			
	esttab , keep(de_jure_NT  search_hat _cons) se(4) b(4) star(* 0.1 ** 0.05 *** 0.01)  stats(c N) 
		
	esttab using "$agetablepath/Search/tab_wage_haz_search_incarc.tex", replace keep(de_jure_NT search_hat  _cons ) booktabs nodepvars noobs nomtitles  ///
	varlabels(de_jure_NT "De jure MAN (months)" search_hat "Search"_cons "Control mean") se(4) b(4) star(* 0.1 ** 0.05 *** 0.01) ///
	stats(Fstat  N_clust N, fmt( %9.2f  %9.0f %9.0fc) /// 
	labels("F-stat"  "\# clusters" "\# observations")) ///
	title("Search efficeny  \label{tab:searcheff}" ) ///
	mlabels("Hazard" " $\Delta $ log($ w $) "  "Pr(Search outside PES) " "Pr(Search anywhere)" "Search intensity" "Arc(Search intensity)"  "Hazard (IV), search outside" "Hazard (IV), search anywhere" "Hazard (IV), search intensity" "Hazard (IV), Arc(search intensity)"  ) ///
	nonotes 

restore	

}
*


{ // V2: SPLIT ON/OFF NOTICE: Residulized wages, hazard with search relative to notification (pooled time)
*===============================================================================
preserve
	
	local search "search_ind"
	
	*keep if nvals==1 | `search'!=.
	keep if inrange(nottime_def,120,180)
	keep if inrange(eventtime_def,-6,9)
	sum nottime_def,d
	
	replace nottime_def = nottime_def/30
	gen defacto_NT = round(nottime_def)
	tab defacto_NT
	
	
	
	*drop if varselorsak==5
	*keep if atfirm_tm1==1
	
	gen eventtime_pool = floor(eventtime_def/2)
	tab eventtime_def eventtime_pool
	

	
	xtset ym_def
	xtreg diff_log_manl_newfirm 	 $covariets  if nvals==1, cluster(varselid)  fe nonest
	predict e_wage ,e
	qui sum diff_log_manl_newfirm if e(sample)==1
	replace e_wage = e_wage + `r(mean)'
	
	gen e_wage_from_notice = e_wage if eventtime_def<defacto_NT
	gen e_wage_off_notice = e_wage if eventtime_def>=defacto_NT
	
	tab eventtime_def if e_wage_from_notice!=.
	tab eventtime_def if e_wage_off_notice!=.
	
	
	xtreg haz 	 $covariets  , cluster(varselid)  fe nonest
	predict e_haz ,e
	qui sum haz if e(sample)==1
	replace e_haz = e_haz + `r(mean)'
	
	gen search_at_risk = `search' if haz==0
	
	gcollapse (mean) e_*  `search' search_at_risk (semean) SE_e_wage_from=e_wage_from_notice SE_e_wage_off=e_wage_off_notice SE_haz=e_haz (count) N=e_wage_off_notice, by(eventtime_pool  )
	tab  N eventtime_pool
	sum e_wage_off_notice SE_e_wage_off  if eventtime_pool==4
	
	gen seu_wage_from = e_wage_from + 1.96*SE_e_wage_from
	gen sel_wage_from = e_wage_from - 1.96*SE_e_wage_from
	
	gen seu_wage_off = e_wage_off + 1.96*SE_e_wage_off
	gen sel_wage_off = e_wage_off - 1.96*SE_e_wage_off
	
	gen seu_haz = e_haz  + 1.96*SE_haz
	gen sel_haz = e_haz  - 1.96*SE_haz
	
	twoway 	(connected `search'  eventtime_pool ,  lpattern(solid) lcolor(black) mcolor(black) ms(o) ) ///
		(connected search_at_risk  eventtime_pool ,  lpattern(dash) lcolor(black) mcolor(black) ms(o) ) ///
		(scatter e_wage_from  eventtime_pool , yaxis(2) lpattern(dash) lcolor(maroon) ms(dh) mcolor(maroon)) ///
		(scatter e_wage_off  eventtime_pool , yaxis(2) lpattern(dash) lcolor(navy) ms(dh) mcolor(navy)) ///
		(scatter e_haz  eventtime_pool , yaxis(2) lpattern(dash) lcolor(black) ms(oh) mcolor(black)) ///
		(rcap seu_wage_from sel_wage_from eventtime_pool, yaxis(2) lpattern(dash) lstyle(foreground) lcolor(maroon) ) ///
		(rcap seu_wage_off sel_wage_off eventtime_pool, yaxis(2) lpattern(dash) lstyle(foreground) lcolor(navy) ) ///
		(rcap seu_haz sel_haz eventtime_pool, yaxis(2) lpattern(dash) lstyle(foreground) lcolor(black) ) ///
	,graphregion(color(white))  legend(order(1 "Search" 2 "Search (at risk)" 3 "{&Delta}log(wage) (during notice)" 4 "{&Delta}log(wage) (after notice)" 5 "Hazard") col(2)  region(color(white))) /// 
	xtitle("Months relative to notification") ytitle("Search") ytitle("{&Delta}ln(w) / Hazard", axis(2))  xscale(r(-3 4)) ///
	xlab(-3 "-6 to -5" -2 "-4 to -3" -1 "-2 to -1" 0 "0-1" 1 "2-3" 2 "4-5" 3 "6-7" 4 "8-9" ) ///
	name(b,replace) xline(0, lpattern(dash) lstyle(foreground) lcolor(black)) yline(0, lpattern(dash) lcolor(gs9) lstyle(foreground) axis(2))
	
	graph export "$agegraphpath/20230802_wages_hazard_and_searchint_defacto_to9_splitwage_v2round.pdf",replace
restore	
}
*

{ // Residulized wages AND hazard with search relative to first report (discrete time)
*===============================================================================
preserve
	
	local search "search_ind"
	
	keep if inrange(de_jure_NT,2,6)
	*keep if inrange(eventtime_ankomst,-6,ankomst_inkom_time_round+2)
	keep if inrange(eventtime_ankomst,-6,9)
	drop if varselorsak==5

	gen eventtime_pool = floor(eventtime_ankomst)
	tab eventtime_ankomst eventtime_pool
	
	
	
	xtset ym_def
	xtreg diff_log_manl_newfirm_ankomst 	 $covariets  if nvals==1, cluster(varselid)  fe nonest
	predict e_wage ,e
	qui sum diff_log_manl_newfirm if e(sample)==1
	replace e_wage = e_wage + `r(mean)'
	
	
	tab eventtime_ankomst if e_wage!=.
	
	xtreg haz 	 $covariets  , cluster(varselid)  fe nonest
	predict e_haz ,e
	qui sum haz if e(sample)==1
	replace e_haz = e_haz + `r(mean)'
	
	gen search_at_risk = `search' if haz==0
	
	gcollapse (mean) e_*  `search' search_at_risk (semean) SE_wage=e_wage SE_haz=e_haz (count) N=e_wage, by(eventtime_pool  )
	tab N  eventtime_pool
	sum e_wage SE_wage  if eventtime_pool==4
	gen seu_wage = e_wage + 1.96*SE_wage
	gen sel_wage = e_wage - 1.96*SE_wage
	gen seu_haz = e_haz  + 1.96*SE_haz
	gen sel_haz = e_haz  - 1.96*SE_haz
	
	twoway 	(connected `search'  eventtime_pool ,  lpattern(solid) lcolor(black) mcolor(black) ms(o) ) ///
		(connected search_at_risk  eventtime_pool ,  lpattern(dash) lcolor(black) mcolor(black) ms(o) ) ///
		(scatter e_wage  eventtime_pool , yaxis(2) lpattern(dash) lcolor(black) ms(dh) mcolor(black)) ///
		(scatter e_haz  eventtime_pool , yaxis(2) lpattern(dash) lcolor(black) ms(oh) mcolor(black)) ///
		(rcap seu_wage sel_wage eventtime_pool, yaxis(2) lpattern(dash) lstyle(foreground) lcolor(black) ) ///
		(rcap seu_haz sel_haz eventtime_pool, yaxis(2) lpattern(dash) lstyle(foreground) lcolor(black) ) ///
	,graphregion(color(white))  legend(order(1 "Search" 2 "Search (at risk)" 3 "{&Delta}log(wage)" 4 "Hazard") col(2)  region(color(white))) /// 
	xtitle("Months relative to first layoff report") ytitle("Search") ytitle("{&Delta}ln(w) / Hazard", axis(2))  xscale(r(-3 4)) ///
	xlab(-6(1)9) ///
	name(a2,replace) xline(0, lpattern(dash) lstyle(foreground) lcolor(black)) yline(0, lpattern(dash) lcolor(gs9) lstyle(foreground) axis(2))
	
	graph export "$agegraphpath/wages_hazard_and_searchint_dejure_to9.pdf",replace
	*xlab(-3 "-6 to -5" -2 "-4 to -3" -1 "-2 to -1" 0 "0-1" 1 "2-3" 2 "4-5" 3 "6-7" 4 "8-9" ) ///
restore	
}
*

{ // V2: SPLIT ON/OFF NOTICE: Residulized wages AND hazard with search relative to first report (discrete time)
*===============================================================================
preserve
	
	local search "search_ind"
	
	keep if inrange(de_jure_NT,2,6)
	*keep if inrange(eventtime_ankomst,-6,ankomst_inkom_time_round+2)
	keep if inrange(eventtime_ankomst,-6,9)
	drop if varselorsak==5

	gen eventtime_pool = floor(eventtime_ankomst)
	tab eventtime_ankomst eventtime_pool
	
	
	
		
	xtset ym_def
	xtreg diff_log_manl_newfirm_ankomst 	 $covariets  if nvals==1, cluster(varselid)  fe nonest
	predict e_wage ,e
	qui sum diff_log_manl_newfirm_ankomst if e(sample)==1
	replace e_wage = e_wage + `r(mean)'
	
	gen e_wage_from_notice = e_wage if eventtime_ankomst <de_jure_NT + ankomst_inkom_time_round
	gen e_wage_off_notice = e_wage if eventtime_ankomst >=de_jure_NT + ankomst_inkom_time_round
	
	tab eventtime_ankomst if e_wage_from_notice!=.
	tab eventtime_ankomst if e_wage_off_notice!=.
	
	
	tab eventtime_ankomst if e_wage!=.
	
	xtreg haz 	 $covariets  , cluster(varselid)  fe nonest
	predict e_haz ,e
	qui sum haz if e(sample)==1
	replace e_haz = e_haz + `r(mean)'
	
	gen search_at_risk = `search' if haz==0
	
	
	gcollapse (mean) e_*  `search' search_at_risk (semean) SE_e_wage_from=e_wage_from_notice SE_e_wage_off=e_wage_off_notice SE_haz=e_haz (count) N=e_wage_off_notice, by(eventtime_pool  )
	tab  N eventtime_pool
	sum e_wage_off_notice SE_e_wage_off  if eventtime_pool==4
	
	gen seu_wage_from = e_wage_from + 1.96*SE_e_wage_from
	gen sel_wage_from = e_wage_from - 1.96*SE_e_wage_from
	
	gen seu_wage_off = e_wage_off + 1.96*SE_e_wage_off
	gen sel_wage_off = e_wage_off - 1.96*SE_e_wage_off
	
	gen seu_haz = e_haz  + 1.96*SE_haz
	gen sel_haz = e_haz  - 1.96*SE_haz
	
	twoway 	(connected `search'  eventtime_pool ,  lpattern(solid) lcolor(black) mcolor(black) ms(o) ) ///
		(connected search_at_risk  eventtime_pool ,  lpattern(dash) lcolor(black) mcolor(black) ms(o) ) ///
		(scatter e_wage_from  eventtime_pool , yaxis(2) lpattern(dash) lcolor(maroon) ms(dh) mcolor(maroon)) ///
		(scatter e_wage_off  eventtime_pool , yaxis(2) lpattern(dash) lcolor(navy) ms(dh) mcolor(navy)) ///
		(scatter e_haz  eventtime_pool , yaxis(2) lpattern(dash) lcolor(black) ms(oh) mcolor(black)) ///
		(rcap seu_wage_from sel_wage_from eventtime_pool, yaxis(2) lpattern(dash) lstyle(foreground) lcolor(maroon) ) ///
		(rcap seu_wage_off sel_wage_off eventtime_pool, yaxis(2) lpattern(dash) lstyle(foreground) lcolor(navy) ) ///
		(rcap seu_haz sel_haz eventtime_pool, yaxis(2) lpattern(dash) lstyle(foreground) lcolor(black) ) ///
	,graphregion(color(white))  legend(order(1 "Search" 2 "Search (at risk)" 3 "{&Delta}log(wage) (during notice)" 4 "{&Delta}log(wage) (after notice)" 5 "Hazard") col(2)  region(color(white))) /// 
	xtitle("Months relative to first layoff report") ytitle("Search") ytitle("{&Delta}ln(w) / Hazard", axis(2))  xscale(r(-3 4)) ///
	name(b,replace) xline(0, lpattern(dash) lstyle(foreground) lcolor(black)) yline(0, lpattern(dash) lcolor(gs9) lstyle(foreground) axis(2))
	
	*xlab(-3 "-6 to -5" -2 "-4 to -3" -1 "-2 to -1" 0 "0-1" 1 "2-3" 2 "4-5" 3 "6-7" 4 "8-9" ) ///
restore	
}
*


{ // V2: SPLIT ON/OFF NOTICE: Residulized wages, hazard with search relative to notification (pooled time)
*===============================================================================
preserve
	
	local search "search_ind"
	
	*keep if nvals==1 | `search'!=.
	keep if inrange(de_jure_NT,2,6)
	keep if inrange(eventtime_ankomst,-6,9)
	drop if varselorsak==5
	
	replace nottime_def = nottime_def/30
	gen defacto_NT = round(nottime_def)
	*keep if atfirm_tm1==1
	
	gen eventtime_pool = floor(eventtime_ankomst/2)
	tab eventtime_ankomst eventtime_pool
	

	
	xtset ym_def
	xtreg diff_log_manl_newfirm_ankomst 	 $covariets  if nvals==1, cluster(varselid)  fe nonest
	predict e_wage ,e
	qui sum diff_log_manl_newfirm_ankomst if e(sample)==1
	replace e_wage = e_wage + `r(mean)'
	
	gen e_wage_from_notice = e_wage if eventtime_ankomst <de_jure_NT + ankomst_inkom_time_ceil
	gen e_wage_off_notice = e_wage if eventtime_ankomst >=de_jure_NT + ankomst_inkom_time_ceil
	
	tab eventtime_ankomst if e_wage_from_notice!=.
	tab eventtime_ankomst if e_wage_off_notice!=.
	
	
	xtreg haz 	 $covariets  , cluster(varselid)  fe nonest
	predict e_haz ,e
	qui sum haz if e(sample)==1
	replace e_haz = e_haz + `r(mean)'
	
	gen search_at_risk = `search' if haz==0
	
	gcollapse (mean) e_*  `search' search_at_risk (semean) SE_e_wage_from=e_wage_from_notice SE_e_wage_off=e_wage_off_notice SE_haz=e_haz (count) N=e_wage_off_notice, by(eventtime_pool  )
	tab  N eventtime_pool
	sum e_wage_off_notice SE_e_wage_off  if eventtime_pool==4
	
	gen seu_wage_from = e_wage_from + 1.96*SE_e_wage_from
	gen sel_wage_from = e_wage_from - 1.96*SE_e_wage_from
	
	gen seu_wage_off = e_wage_off + 1.96*SE_e_wage_off
	gen sel_wage_off = e_wage_off - 1.96*SE_e_wage_off
	
	gen seu_haz = e_haz  + 1.96*SE_haz
	gen sel_haz = e_haz  - 1.96*SE_haz
	
	twoway 	(connected `search'  eventtime_pool ,  lpattern(solid) lcolor(black) mcolor(black) ms(o) ) ///
		(connected search_at_risk  eventtime_pool ,  lpattern(dash) lcolor(black) mcolor(black) ms(o) ) ///
		(scatter e_wage_from  eventtime_pool , yaxis(2) lpattern(dash) lcolor(maroon) ms(dh) mcolor(maroon)) ///
		(scatter e_wage_off  eventtime_pool , yaxis(2) lpattern(dash) lcolor(navy) ms(dh) mcolor(navy)) ///
		(scatter e_haz  eventtime_pool , yaxis(2) lpattern(dash) lcolor(black) ms(oh) mcolor(black)) ///
		(rcap seu_wage_from sel_wage_from eventtime_pool, yaxis(2) lpattern(dash) lstyle(foreground) lcolor(maroon) ) ///
		(rcap seu_wage_off sel_wage_off eventtime_pool, yaxis(2) lpattern(dash) lstyle(foreground) lcolor(navy) ) ///
		(rcap seu_haz sel_haz eventtime_pool, yaxis(2) lpattern(dash) lstyle(foreground) lcolor(black) ) ///
	,graphregion(color(white))  legend(order(1 "Search" 2 "Search (at risk)" 3 "{&Delta}log(wage) (during notice)" 4 "{&Delta}log(wage) (after notice)" 5 "Hazard") col(2)  region(color(white))) /// 
	xtitle("Months relative to first layoff report") ytitle("Search") ytitle("{&Delta}ln(w) / Hazard", axis(2))  xscale(r(-3 4)) ///
	xlab(-3 "-6 to -5" -2 "-4 to -3" -1 "-2 to -1" 0 "0-1" 1 "2-3" 2 "4-5" 3 "6-7" 4 "8-9" ) ///
	name(b,replace) xline(0, lpattern(dash) lstyle(foreground) lcolor(black)) yline(0, lpattern(dash) lcolor(gs9) lstyle(foreground) axis(2))
	
	graph export "$agegraphpath/20230802_wages_hazard_and_searchint_defacto_to9_splitwage_v2round.pdf",replace
restore	
}
*

{ // Residulized wages AND hazard with search relative to notification (pooled time)
*===============================================================================
preserve
	
	local search "search_ind"
	
	*keep if nvals==1 | `search'!=.
	keep if inrange(nottime_def,120,180)
	keep if inrange(eventtime_def,-6,9)

	sum nottime_def,d
	

	gen eventtime_pool = floor(eventtime_def/2)
	tab eventtime_def eventtime_pool
	
	
	
	xtset ym_def
	xtreg diff_log_manl_newfirm 	 $covariets  if nvals==1, cluster(varselid)  fe nonest
	predict e_wage ,e
	qui sum diff_log_manl_newfirm if e(sample)==1
	replace e_wage = e_wage + `r(mean)'
	
	
	tab eventtime_def if e_wage!=.
	sum e_wage if inrange(eventtime_def,8,9)
	
	xtreg haz 	 $covariets  , cluster(varselid)  fe nonest
	predict e_haz ,e
	qui sum haz if e(sample)==1
	replace e_haz = e_haz + `r(mean)'
	
	gen search_at_risk = `search' if haz==0
	
	gcollapse (mean) e_*  `search' search_at_risk (semean) SE_wage=e_wage SE_haz=e_haz (count) N=e_wage, by(eventtime_pool  )
	tab N  eventtime_pool
	sum e_wage SE_wage  if eventtime_pool==4
	gen seu_wage = e_wage + 1.96*SE_wage
	gen sel_wage = e_wage - 1.96*SE_wage
	gen seu_haz = e_haz  + 1.96*SE_haz
	gen sel_haz = e_haz  - 1.96*SE_haz
	
	twoway 	(connected `search'  eventtime_pool ,  lpattern(solid) lcolor(black) mcolor(black) ms(o) ) ///
		(connected search_at_risk  eventtime_pool ,  lpattern(dash) lcolor(black) mcolor(black) ms(o) ) ///
		(scatter e_wage  eventtime_pool , yaxis(2) lpattern(dash) lcolor(black) ms(dh) mcolor(black)) ///
		(scatter e_haz  eventtime_pool , yaxis(2) lpattern(dash) lcolor(black) ms(oh) mcolor(black)) ///
		(rcap seu_wage sel_wage eventtime_pool, yaxis(2) lpattern(dash) lstyle(foreground) lcolor(black) ) ///
		(rcap seu_haz sel_haz eventtime_pool, yaxis(2) lpattern(dash) lstyle(foreground) lcolor(black) ) ///
	,graphregion(color(white))  legend(order(1 "Search" 2 "Search (at risk)" 3 "{&Delta}log(wage)" 4 "Hazard") col(2)  region(color(white))) /// 
	xtitle("Months relative to notification") ytitle("Search") ytitle("{&Delta}ln(w) / Hazard", axis(2))  xscale(r(-3 4)) ///
	xlab(-3 "-6 to -5" -2 "-4 to -3" -1 "-2 to -1" 0 "0-1" 1 "2-3" 2 "4-5" 3 "6-7" 4 "8-9" ) ///
	name(a2,replace) xline(0, lpattern(dash) lstyle(foreground) lcolor(black)) yline(0, lpattern(dash) lcolor(gs9) lstyle(foreground) axis(2))
	
	graph export "$agegraphpath/20230802_wages_hazard_and_searchint_defacto_to9.pdf",replace
restore	
}
*
